var winston=require('winston');
var expressWinston=require('express-winston');

module.exports=(app)=>{
    //请求日志
    app.use(expressWinston.logger({
        transports:[
            /*        new (winston.transports.Console)({
             json:true,
             colorize:true
             }),*/
            new winston.transports.File({
                filename:'./logs/success.log'
            })
        ]
    }));

    //路由
    app.get('/',(req,res)=>{
        res.redirect('/posts');
    });
    app.use('/signup',require('./signup'));
    app.use('/signin',require('./signin'));
    app.use('/signout',require('./signout'));
    app.use('/posts',require('./posts'));
    app.use('/notify',require('./notify'));

    //404
    app.use((req,res)=>{
        if(!res.headersSent){
            res.status(404).render('404');
        }
    });

    //错误请求日志
    app.use(expressWinston.errorLogger({
        transports:[
            /*new winston.transports.Console({
                json: true,
                colorize: true
            }),*/
            new winston.transports.File({
                filename:'./logs/error.log'
            })
        ]
    }));

    //错误请求处理
    app.use((err,req,res,next)=>{
        console.error(err);
        res.status(500).render('error');
    });
};